<template>
	<view class="signup">
		<view class="sig_h">
			<image :src="activtyInfo.url" class="sig_himg"></image>
			<view class="sig_hv">
				<text class="sig_hvt">{{ activtyInfo.title }}</text>
				<text class="sig_hvc">特来邀请，期待您的参与</text>
			</view>
		</view>
		<view class="sig_c">
			<image :src="activtyInfo.url" class="sig_cimg"></image>
			<view class="sig_cl">
				<view class="sig_clc">{{ activtyInfo.location }}</view>
			</view>
			<view class="sig_ci">
				<text class="sig_cic">{{ activtyInfo.beginTime }}</text>
				<text class="sig_cic">{{ zhi }}</text>
				<text class="sig_cic">{{ activtyInfo.endTime }}</text>
			</view>
			<view class="sig_cc">
				<text class="sig_ccn">{{ activtyInfo.content }}</text>
			</view>
			<!-- 表单 -->
			<view class="sig_cf">
				<view class="sig_cfi">
					<label><text style="color: #FF0000;">*</text>参与人员：</label>
					<input placeholder="请输入参与人员姓名" v-model="name" />
				</view>
				<view class="sig_cfi">
					<label><text style="color: #FF0000;">*</text>手机号码：</label>
					<input placeholder="请输入参与人手机号码" v-model="number" @input="handleInput" />
				</view>
				<text style="color: #FF0000;margin-left: 130rpx;" v-show="!isValid">手机号码格式不对</text>
				<view class="sig_cfi">
					<label><text style="color: #FF0000;">*</text>参与单位：</label>
					<input placeholder="请输入参与单位" v-model="unit" />
				</view>
				<view class="sig_cfb" @click="subSignupData">提交</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		debounce
	} from "@/utils/common";
	import {
		postSaveOrUpdateCompete,
		getUserActivityList
	} from "@/proxy/activity/activity.api.js";
	export default {
		data() {
			return {
				name: "", // 姓名
				number: "", // 手机号
				unit:"",
				isValid:true,
				activtyId:"" , // 活动id
				activtyInfo:{},
				zhi:"\u0020至\u0020"
			}
		},
		async onLoad(option) {
			const query = this.getQueryParams(option.q) // 获取到二维码原始链接内容
			// const scancode_time = parseInt(option.scancode_time) // 获取用户扫码时间 UNIX 时间戳
			this.activtyId = query.aid;
			await this.getUserActivityListData(query.aid);
			// await this.getUserActivityListData(10);
			// console.log(option)
			// uni.showLoading({
			// 	title: `正在生成海报${query.aid}`,
			// 	icon: 'loading'
			// })
		},
		methods: {
			async getUserActivityListData(id) {
				try {
					const result = await getUserActivityList({
						activtyId:id
					});
					console.log("用户查询活动列表", result)
					if (result.code == 200) {
						this.activtyInfo = result.data[0];
					} else {
						return this.$toast({
							title: result.data.msg,
						});
					}
				} catch (error) {
					console.error("请求失败", error);
					return this.$toast({
						title: "发起失败，请联系管理员！",
						icon: "error",
					});
				}
			},
			handleInput:debounce(async function(e){
				const phoneRegex = /^134[0-8]\d{7}$|^13[^4]\d{8}$|^14[5-9]\d{8}$|^15[^4]\d{8}$|^l6[6]\d{8}$|^17[0-8]\d{8}$|^18[\d]{9}$|^19[8,9]\d{8}$/;
				// 如果是有效手机号，返回 true，否则返回 false
				this.isValid = phoneRegex.test(e.detail.value);
			}, 500),
			getQueryParams: function(queryString) {
				// 微信扫码得到的内容进行了一次编码，所以官方要求需要进行decodeURIComponent一次
				queryString = decodeURIComponent(queryString)
				let params = {}
				if (queryString) {
					let queryArray = queryString.split('?')
					if (queryArray.length > 1) {
						let query = queryArray[1]
						let array = query.split('&')
						array.map((value) => {
							let valueArray = value.split('=')
							if (valueArray.length > 1) {
								// 还需要对value进行解码（可能涉及到在value为中文字符，在赋值到二维码前进行encodeURI编码）
								Object.assign(params, {
									[valueArray[0]]: decodeURI(valueArray[1])
								})
							}
						})
					}
				}
				return params
			},
			// 提交报名
			async subSignupData(){
				var that = this;
				if(!that.activtyId){
					return that.$toast({
						title:"改活动已过期"
					})
				}
				if(!that.name){
					return that.$toast({
						title:"请输入参与人员姓名"
					})
				}
				if(!that.number){
					return that.$toast({
						title:"请输入参与人手机号码"
					})
				}
				if(!that.isValid){
					return that.$toast({
						title:"手机号码格式不对"
					})
				}
				if(!that.unit){
					return that.$toast({
						title:"请输入参与单位"
					})
				}
				
				// 将 uni.showModal 封装为 Promise
				const confirmSubmit = () => {
					return new Promise((resolve, reject) => {
						uni.showModal({
							title: '确认提交',
							content: '你确定要参加该活动吗？',
							success: function(res) {
								if (res.confirm) {
									resolve(); // 用户点击确认，继续执行提交操作
								} else if (res.cancel) {
									reject('用户取消提交'); // 用户点击取消，返回失败
								}
							}
						});
					});
				};
				
				try {
					// 等待用户确认
					await confirmSubmit();
				
					// 用户点击确认后执行的异步操作
					const result = await postSaveOrUpdateCompete({
						activtyId:that.activtyId,
						name:that.name,
						number:that.number,
						unit:that.unit
					});
				
					console.log("参加活动结果", result);
					if (result.code == 200) {
						// 提交成功后的操作
						that.$toast({
							title: "提交成功",
							icon: "success",
							success: () => {
								var nav = setTimeout(() => {
									clearTimeout(nav)
									// 传递对象时将其转为 JSON 字符串
									let obj = { id: that.activtyInfo.id, title: that.activtyInfo.title, location: that.activtyInfo.location, content: that.activtyInfo.content, url: that.activtyInfo.url, number: that.number, name: that.name, unit: that.unit };
									
									uni.navigateTo({
									  url: '/pagesActivity/activity/vouchers?data=' + encodeURIComponent(JSON.stringify(obj))
									});
								}, 500)
							}
						});
						
					} else {
						that.$toast({
							title: result.data.msg,
						});
					}
				
				} catch (err) {
					// 捕获用户取消或者提交失败的错误
					if (err === '用户取消提交') {
						console.log('用户取消提交');
					} else {
						that.$toast({
							icon: 'none',
							title: '发起失败，请联系管理员！',
							duration: 2000
						});
						console.log(err);
					}
				}
				
			}
		}
	}
</script>

<style lang="scss">
	.signup{
		width: 750rpx;
		padding: 40rpx;
		box-sizing: border-box;
		background: linear-gradient(to top,#000 0%, #2698fb 100%);
		.sig_h{
			display: flex;
			align-items: center;
			.sig_himg{
				background: #fff;
				object-fit: cover;
				// margin-left: 40rpx;
				// margin-top: 40rpx;
				width: 84rpx;
				border: 2rpx solid #fff;
				box-sizing: border-box;
				height: 84rpx;
				border-radius: 50%;
			}
			.sig_hv{
				flex: 1;
				padding-left: 20rpx;
				.sig_hvt{
					// display: inline-block;
					margin-bottom: 10rpx;
					color: #fff;
					-webkit-line-clamp: 1; /* 需要加上 -webkit 前缀，支持line-clamp效果 */
					line-clamp: 1; /* 这个是标准属性，部分浏览器需要加前缀 */
					font-size: 32rpx;
					font-weight: bold;
					display: -webkit-box;
					-webkit-box-orient: vertical;
					overflow: hidden;
				}
				.sig_hvc{
					display: block;
					color: rgba(255,255,255,.7);
					font-size: 24rpx;
				}
			}
		}
		.sig_c{
			// margin-left: 40rpx;
			margin-top: 30rpx;
			padding: 32rpx;
			box-sizing: border-box;
			background: #fff;
			border-radius: 16rpx;
			width: 670rpx;
			box-shadow: 0 20rpx 58rpx rgba(0, 0, 0, .15);
			.sig_cimg{
				object-fit: cover;
				object-position: 50% 50%;
				width: 606rpx;
				height: 406rpx;
			}
			.sig_cl{
				margin-top: 30rpx;
				.sig_clc{
					color: #2698fb;
					border: 1rpx solid #2698fb;
					padding: 10rpx 16rpx;
					font-size: 24rpx;
				}
			}
			.sig_ci{
				margin: 30rpx 0; 
				color: #999999;
				font-size: 28rpx;    
				line-height: 1em;
				.sig_cic{
					vertical-align: bottom;
				}
			}
			.sig_cc{
				margin-bottom: 30rpx;
				.sig_ccn{
					padding-right: 32rpx;
					margin-top: 16rpx;
					box-sizing: border-box;
					-webkit-line-clamp: 2; /* 需要加上 -webkit 前缀，支持line-clamp效果 */
					line-clamp: 2; /* 这个是标准属性，部分浏览器需要加前缀 */
					display: -webkit-box;
					-webkit-box-orient: vertical;
					overflow: hidden;
					color: #333333;
					line-height: 48rpx;
					font-size: 30rpx;
				}
			}
			.sig_cf{
				background-color: #efeff4;
				padding: 10rpx;
				border-radius: 10rpx;
				font-size: 27rpx;
				.sig_cfi{
					display: flex;
					align-items: center;
					border-bottom: 1rpx solid #ddd;
					padding: 20rpx 0;
				}
				.sig_cfb{
					width: 290rpx;
					height: 65rpx;
					line-height: 65rpx;
					color: #fff;
					background-color: #2698fb;
					margin: 15rpx auto;
					text-align: center;
					border-radius: 15rpx;
				}
			}
		}
	}
</style>